package com.dimong.www.loan.ui.frag.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.loadmore.SwipeRefreshHelper;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.dimong.loan.sdk.net.NetHttpCallback;
import com.dimong.loan.sdk.ui.adapter.BaseRecyclerAdapter;
import com.dimong.www.loan.R;
import com.dimong.www.loan.bean.CmsBean;
import com.dimong.www.loan.bean.MainBean;
import com.dimong.www.loan.net.LoadHttpUtil;
import com.dimong.www.loan.net.response.cms.GetCmsList;
import com.dimong.www.loan.ui.UIHelper;
import com.dimong.www.loan.ui.adapter.main.MainAdapter;
import com.dimong.www.loan.ui.base.BaseRefreshFragment;
import com.dimong.www.loan.ui.base.TitleActivity;
import com.dimong.www.loan.ui.frag.CmsContentFrag;
import com.dimong.www.loan.widget.ActionBarView;
import com.dimong.www.loan.widget.LoanRefreshView;

public class MainThreeFrag extends BaseRefreshFragment {
    private ActionBarView title;
    private SwipeRefreshLayout srl_frag_main_three;
    private RecyclerView rv_frag_main_three;
    private MainAdapter mainAdapter;
    private SwipeRefreshHelper mSwipeRefreshHelper;
    private int page;

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        title = (ActionBarView) findViewById(R.id.title);
        srl_frag_main_three = (SwipeRefreshLayout) findViewById(R.id.srl_frag_main_three);
        rv_frag_main_three = (RecyclerView) findViewById(R.id.rv_frag_main_three);
    }

    @Override
    public void setViewStatus() {
        title.setTitle("资讯");
        title.setLeftImgVisible(View.GONE);
        rv_frag_main_three.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainAdapter = new MainAdapter();
        rv_frag_main_three.setAdapter(new RecyclerAdapterWithHF(mainAdapter));
        mSwipeRefreshHelper = new SwipeRefreshHelper(srl_frag_main_three, rv_frag_main_three);
        mSwipeRefreshHelper.setLoadMoreEnable(true);
        mSwipeRefreshHelper.setOnSwipeRefreshListener(new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onfresh() {
                page = 1;
                getData();
            }
        });

        mSwipeRefreshHelper.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                getData();
            }
        });

        mainAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                MainBean mainBean = (MainBean) mainAdapter.getItem(position);
                if (mainBean.getObject() instanceof CmsBean) {
                    CmsBean bean = (CmsBean) mainBean.getObject();
                    if ("0".equals(bean.getJump_type())) {
                        UIHelper.openWebViewAty(getActivity(), bean.getTitle(), bean.getHref());
                    }
                    if ("1".equals(bean.getJump_type())) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("cmsBean", bean);
                        Intent intent = new Intent(getActivity(), TitleActivity.class);
                        intent.putExtra(":fragment_name", CmsContentFrag.class.getName());
                        intent.putExtra(":fragment_args", bundle);
                        getActivity().startActivity(intent);
                    }
                }
            }
        });
        page = 1;
        showRefreshLoading();
        getData();

        setLoanReloadClick(new LoanRefreshView.LoanReloadClick() {
            @Override
            public void onRefreshClick() {
                page = 1;
                showRefreshLoading();
                getData();
            }
        });
    }

    public void getData() {
        LoadHttpUtil.getCmsList(page, new NetHttpCallback() {
            @Override
            public void onResult(String result) {
                mSwipeRefreshHelper.refreshComplete();
                super.onResult(result);
                GetCmsList getCmsList = gson.fromJson(result, GetCmsList.class);
                showRefreshSuccess();
                srl_frag_main_three.setVisibility(View.VISIBLE);
                setData(getCmsList);
            }

            @Override
            public void onError(String errorCode, String error) {
                super.onError(errorCode, error);
                mSwipeRefreshHelper.refreshComplete();
                if (mainAdapter.getItemCount() == 0) {
                    showRefreshError();
                }
                showToast(error);
            }
        });
    }

    private void setData(GetCmsList getCmsList) {
        if (page == 1) {
            mainAdapter.clear();
        }
        if (getCmsList.getList() != null && getCmsList.getList().size() != 0) {
            for (int i = 0; i < getCmsList.getList().size(); i++) {
                MainBean bean1 = new MainBean();
                bean1.setType(MainAdapter.ITEM_LOAN_STRATEGY);
                bean1.setObject(getCmsList.getList().get(i));
                mainAdapter.addData(bean1);
            }
            mSwipeRefreshHelper.loadMoreComplete(true);
            mSwipeRefreshHelper.setLoadMoreEnable(true);
        } else {
            mSwipeRefreshHelper.loadMoreComplete(false);
            mSwipeRefreshHelper.setLoadMoreEnable(false);
        }
        mainAdapter.updateChange();
    }

    @Override
    public int getViewResourceId() {
        return R.layout.frag_main_three;
    }


    @Override
    public RelativeLayout getContainView() {
        return (RelativeLayout) findViewById(R.id.rl_frag_main_three);
    }
}
